min.dir = '/home1/99/jc152199/brt/output/mintemp2/'
max.dir = '/home1/99/jc152199/brt/output/maxtemp2/'

min.t.files = list.files(min.dir, pattern=c('006','.csv'), recursive=TRUE, full.names=TRUE)
max.t.files = list.files(max.dir, pattern='.csv', recursive=TRUE, full.names=TRUE)

min.model.summary = NULL

for (i in c(1:length(min.t.files)))

	{
	
	t.file = read.csv(min.t.files[i],header=T)
	
	min.model.summary = rbind(min.model.summary, t.file)
	
	}
	
# Close Loop

max.model.summary = NULL

for (i in c(1:length(max.t.files)))

	{
	
	t.file = read.csv(max.t.files[i],header=T)
	
	max.model.summary = rbind(max.model.summary, t.file)
	
	}
	
# Close Loop

 max.model.summary[which(max.model.summary$test.deviance==min(max.model.summary$test.deviance)),]
 min.model.summary[which(min.model.summary$test.deviance==min(min.model.summary$test.deviance)),]
 
 # Look for minimum deviance amongst parameters sets where tf = 1
 

max.model.summary[which(max.model.summary$train.deviance==min(max.model.summary$train.deviance)),] # Has no testing deviance
max.model.summary[which(max.model.summary$test.deviance==min(max.model.summary$test.deviance, na.rm=T)),]
min.model.summary[which(min.model.summary$train.deviance==min(min.model.summary$train.deviance)),]
min.model.summary[which(min.model.summary$test.deviance==min(min.model.summary$test.deviance, na.rm=T)),]

		
#### Need to create some plots from the max model summaries
### The following loop plots learning rate vs CV deviance for each unique combination of n.trees & train.fraction
### Each point on the plots represents a single model, each line on the plot represents a series of models with the same tree.complexity

maxplotdir = '/home1/99/jc152199/brt/output/testplots/maxplots/';setwd(maxplotdir)

tftc = unique(cbind(max.model.summary$train.fraction, max.model.summary$tree.complexity))

xlims = range(max.model.summary$learning.rate) # Xlims
ylims = range(max.model.summary$train.deviance) # Ylims

for (nt in c(unique(max.model.summary$n.trees)))

	{

	t.max.model.summary = max.model.summary[which(max.model.summary$n.trees==nt),]
	
	dir.create(paste(maxplotdir,nt,sep=''))

for (ii in unique(tftc[,1]))

	{
	
	t.tftc = tftc[which(tftc[,1]==ii),]
	
	png(paste(maxplotdir,'/',nt,'/','lr_vs_cvdev_tf=',unique(t.tftc[,1]),'_nt=',nt,'.png',sep=''))
	
	plot(max.model.summary$learning.rate, max.model.summary$train.deviance, xlim=xlims, ylim=ylims, xlab = 'Learning Rate', ylab='CV Deviance', main=paste('Max Model with ',nt,' Trees & TF = ',ii,sep=''),type='n')
	
for (i in c(1:nrow(t.tftc)))

	{
		
	tt.max.model.summary = t.max.model.summary[which(t.max.model.summary$train.fraction==t.tftc[i,1] & t.max.model.summary$tree.complexity==t.tftc[i,2]),]
	
	points(tt.max.model.summary$learning.rate, tt.max.model.summary$train.deviance, col=rainbow(nrow(t.tftc))[i], pch=i, type='p', cex=1.5)
	
	legend(x=.04, y=(ylims[2]-(i*.12)),legend=paste('TC - ',t.tftc[i,2],sep=''),pch=i,col=rainbow(nrow(t.tftc))[i], bty='n')

	}
	
	dev.off()
	
	}
	
	}
	

# Close loop

#### Need to create some plots from the max model summaries
### The following loop plots learning rate vs CV deviance for each unique combination of n.trees & train.fraction
### Each point on the plots represents a single model, each line on the plot represents a series of models with the same tree.complexity

minplotdir = '/home1/99/jc152199/brt/output/testplots/minplots/';setwd(minplotdir)

tftc = unique(cbind(min.model.summary$train.fraction, min.model.summary$tree.complexity))

xlims = range(min.model.summary$learning.rate) # Xlims
ylims = range(min.model.summary$train.deviance) # Ylims

for (nt in c(unique(min.model.summary$n.trees)))

	{

	t.min.model.summary = min.model.summary[which(min.model.summary$n.trees==nt),]
	
	dir.create(paste(minplotdir,nt,sep=''))

for (ii in unique(tftc[,1]))

	{
	
	t.tftc = tftc[which(tftc[,1]==ii),]
	
	png(paste(minplotdir,'/',nt,'/','lr_vs_cvdev_tf=',unique(t.tftc[,1]),'_nt=',nt,'.png',sep=''))
	
	plot(min.model.summary$learning.rate, min.model.summary$train.deviance, xlim=xlims, ylim=ylims, xlab = 'Learning Rate', ylab='CV Deviance', main=paste('Min Model with ',nt,' Trees & TF = ',ii,sep=''),type='n')
	
for (i in c(1:nrow(t.tftc)))

	{
		
	tt.min.model.summary = t.min.model.summary[which(t.min.model.summary$train.fraction==t.tftc[i,1] & t.min.model.summary$tree.complexity==t.tftc[i,2]),]
	
	points(tt.min.model.summary$learning.rate, tt.min.model.summary$train.deviance, col=rainbow(nrow(t.tftc))[i], pch=i, type='p', cex=1.5)
	
	legend(x=.04, y=(ylims[2]-(i*.12)),legend=paste('TC - ',t.tftc[i,2],sep=''),pch=i,col=rainbow(nrow(t.tftc))[i], bty='n')

	}
	
	dev.off()
	
	}
	
	}
	

# Close loop








	
	